"use strict";
cc._RF.push(module, '1e08dCWvWNLBrmykeusTjuz', 'Game');
// script/Game.js

"use strict";

// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
//require("../../creator");
cc.Class({
  "extends": cc.Component,
  properties: {
    back_ground: {
      "default": null,
      type: cc.Node
    },
    ball_site_X: 0,
    ball_prefab: {
      "default": null,
      type: cc.Prefab
    },
    ball_Sprite: {
      "default": null,
      type: cc.Sprite
    },
    ballLink_Sprite: {
      "default": null,
      type: cc.Sprite
    },
    // 第一个boll是否触底
    isFirstBall: false
  },
  onLoad: function onLoad() {
    //开启物理
    cc.director.getPhysicsManager().enabled = true; // 开启碰撞

    cc.director.getCollisionManager().enabled = true;
    cc.director.getActionManager().gravity = cc.v2(0, -2000); //设置重力

    this.init_site_y = -1136 * 0.5 + 0.5 * 115; //是否在游戏中

    this.isActivity = false; //是否开始游戏

    this.isBegin = false; //球数量

    this.allBalls = 1; //设定小球的位置跟路线的位置

    this.ball_Sprite.node.setPosition(cc.v2(this.ball_site_X, this.init_site_y));
    this.ballLink_Sprite.node.setPosition(cc.v2(this.ball_site_X, this.init_site_y)); //路线不显示

    this.ballLink_Sprite.enabled = false; //监听屏幕是否被触碰

    this.node.on(cc.Node.EventType.TOUCH_START, function (event) {
      this.touchStart(event);
    }.bind(this), this);
    this.node.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
      this.touchMove(event);
    }.bind(this), this); //监听是否结束触碰

    this.node.on(cc.Node.EventType.TOUCH_END, function (event) {
      this.touchEnd(event);
    }.bind(this), this);
  },
  // LIFE-CYCLE CALLBACKS:
  start: function start() {},
  touchStart: function touchStart(event) {
    this.ballLink_Sprite.node.setPosition(cc.v2(this.ball_site_X, this.init_site_y)); //this.ballLink_Sprite.enabled = true;
  },
  touchMove: function touchMove(event) {
    this.ballLink_Sprite.enabled = true; //线条旋转

    var touchX = event.touch._point.x;
    this.ballLink_Sprite.node.setRotation(touchX);

    if (this.ballLink_Sprite.node.rotation < 280) {
      this.ballLink_Sprite.node.setRotation(280);
    }

    if (this.ballLink_Sprite.node.rotation > 440) {
      this.ballLink_Sprite.node.setRotation(440);
    }
  },
  touchEnd: function touchEnd(event) {
    if (this.isActivity == false) {
      this.ballLink_Sprite.enabled = false;

      if (this.isBegin == false) {
        this.isBegin = true;
      }
    }

    this.schedule(function () {
      var ball = cc.instantiate(this.ball_prefab);
      this.node.addChild(ball);
      ball.game = this;
      ball.setPosition(cc.v2(this.ball_site_X, this.init_site_y));
      var boxRigidBody = ball.getComponent(cc.RigidBody);
      var angle = -this.ballLink_Sprite.node.rotation - 270;
      var toX = Math.cos(angle * 0.017453293) * 100;
      var toY = Math.sin(angle * 0.017453293) * 100;
      boxRigidBody.linearVelocity = cc.v2(toX * 10, toY * 10);
    }.bind(this), 0.08, this.allBalls - 1);
    this.schedule(function () {
      this.ball_Sprite.enabled = false;
      this.isFirstBoll = false;
    }.bind(this), 0.08 * (this.allBalls - 1), 1);
    this.isActivity = true;
  }
});

cc._RF.pop();